package com.example.module_web.adapter;

import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.example.module_web.viewholder.BindViewHolder;

import java.util.List;

/**
 * Created by ASUS on 2018/3/17.
 */

public abstract class FormBAdapter<T> extends RecyclerView.Adapter<BindViewHolder> {
    private static final String TAG = "bug";
    private int mLayoutRes;
    private List<T> mBeans;
    private T mBean;
    public FormBAdapter(@LayoutRes int layoutRes, List<T> beans){
        this.mLayoutRes = layoutRes;
        this.mBeans = beans;
    }
    @Override
    public BindViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BindViewHolder(LayoutInflater.from(parent.getContext()).inflate(mLayoutRes,parent,false));
    }

    @Override
    public void onBindViewHolder(BindViewHolder holder, int position) {
        Log.e(TAG, "onBindViewHolder: bean = "+mBeans.get(position));
        onBind(holder,position,mBeans.get(position));
    }

    protected abstract void onBind(BindViewHolder holder, int position, T t);


    @Override
    public int getItemCount() {
        return mBeans.size();
    }
    public T addItem(List<T> items){
        try{
            items.add(0,mBean);
        }catch (NullPointerException ignored){
            Log.e(TAG, "addItem: 数据为空");
            ignored.printStackTrace();
        }
        notifyDataSetChanged();
        return mBean;
    }
}
